import { asyncRouterMap, constantRouterMap } from '@/router'
import instance from '@/api/index.js'
const permission = {
  state: {
    routers: constantRouterMap,
    addRouters: [],
  },
  mutations: {
    SET_ROUTERS: (state, routers) => {
      state.addRouters = routers
      state.routers = constantRouterMap.concat(routers)
    },
  },
  actions: {
    GenerateRoutes({ commit }, data) {
      return new Promise(resolve => {
        if (data) {
          instance.post('/menu/menuList').then(function (data) {
            let add = [];
            for (let i = 0; i < data.data.data.length; i++){
              for (let j = 0; j < asyncRouterMap.length; j++){
                if (data.data.data[i].url == asyncRouterMap[j].children[0].path){
                  add.push(asyncRouterMap[j])
                }
              }
            }
            commit('SET_ROUTERS', add);
            resolve(data)
          })
        }

      })
    }
  }
}

export default permission
